<!DOCTYPE html>
<html lang="zh-cn"><head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/main.css" rel="stylesheet">

    <link rel="shortcut icon" type="image/x-icon" href="/img/favicon.ico" />

    <title>golang的build命令编译 | wiseAI的小站</title>
</head><body><div class="container-fluid">
    <nav class="navbar fixed-top navbar-expand-sm navbar-dark bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="/">
                <img src="/img/favicon.ico" alt="" width="30" height="24" class="d-inline-block align-text-top">
                wiseAI的小站
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/articles/">文章</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/categories/">分类</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/tags/">关键字</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/post/golang/">golang编程</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/about/">关于</a>
                    </li>
                    
                </ul>
                <form class="d-flex">
                    <input id="search-query" class="form-control me-2" type="search" placeholder="Search for anything..." aria-label="Search">
                </form>
            </div>
        </div>
    </nav>
</div>




<div id="content">
<div class="container article">
	<h1>golang的build命令编译</h1>
	<div class="post-meta">
		<div>
			
			
			By wiseai | 2022-05-30 | 1 minutes
		</div>
		<div class="tags">
			
			<a class="btn btn-light links btn-sm" href="/tags/golang/">golang</a>
			
		</div>
	</div>
	<div>
		<div class="article-post">
			<p><code>go build</code>，是我们非常常用的命令，它可以启动编译，把我们的包和相关的依赖编译成一个可执行的文件。</p>
<p><code>usage: go build [-o output] [-i] [build flags] [packages]</code></p>
<p><code>go build</code>的使用比较简洁，所有的参数都可以忽略，直到只有<code>go build</code>，这个时候意味着使用当前目录进行编译，下面的几条命令是等价的：</p>
<pre tabindex="0"><code>go build

go build .

go build hello.go
</code></pre><p>以上这三种写法，都是使用当前目录编译的意思。因为我们忽略了<code>packages</code>,所以自然就使用当前目录进行编译了。从这里我们也可以推测出，<code>go build</code>本质上需要的是一个路径，让编译器可以找到哪些需要编译的go文件。<code>packages</code>其实是一个相对路径，是相对于我们定义的<code>GOROOT</code>和<code>GOPATH</code>这两个环境变量的，所以有了<code>packages</code>这个参数后，<code>go build</code>就可以知道哪些需要编译的go文件了。</p>
<p><code>go build flysnow.org/tools</code></p>
<p>这种方式是指定包的方式，这样会明确地编译我们这个包。当然我们也可以使用通配符</p>
<p><code>go build flysnow.org/tools/...</code></p>
<p><strong>3个点表示匹配所有字符串，这样<code>go build</code>就会编译tools目录下的所有包。</strong></p>
<p>讲到<code>go build</code>编译，不能不提跨平台编译，Go提供了编译链工具，可以让我们在任何一个开发平台上，编译出其他平台的可执行文件。</p>
<p>默认情况下，都是根据我们当前的机器生成的可执行文件，比如你的是Linux 64位，就会生成Linux 64位下的可执行文件，比如我的Mac；可以使用go env查看编译环境,以下截取重要的部分。</p>
<pre tabindex="0"><code>go env
GOARCH=&#34;amd64&#34;
GOEXE=&#34;&#34;
GOHOSTARCH=&#34;amd64&#34;
GOHOSTOS=&#34;darwin&#34;
GOOS=&#34;darwin&#34;
GOROOT=&#34;/usr/local/go&#34;
GOTOOLDIR=&#34;/usr/local/go/pkg/tool/darwin_amd64&#34;
</code></pre><p><strong>注意里面两个重要的环境变量GOOS和GOARCH,其中GOOS指的是目标操作系统，它的可用值为：</strong></p>
<ol>
<li>darwin</li>
<li>freebsd</li>
<li>linux</li>
<li>windows</li>
<li>android</li>
<li>dragonfly</li>
<li>netbsd</li>
<li>openbsd</li>
<li>plan9</li>
<li>solaris</li>
</ol>
<p><strong>一共支持10种操作系统。GOARCH指的是目标处理器的架构，目前支持的有：</strong></p>
<ol>
<li>arm</li>
<li>arm64</li>
<li>386</li>
<li>amd64</li>
<li>ppc64</li>
<li>ppc64le</li>
<li>mips64</li>
<li>mips64le</li>
<li>s390x</li>
</ol>
<p>一共支持9种处理器的架构</p>
<p>如果我们要生成不同平台架构的可执行程序，只要改变这两个环境变量就可以了，比如要生成Linux 64位的程序，命令如下：</p>
<p><code>GOOS=linux GOARCH=amd64 go build flysnow.org/hello</code></p>
<p>前面两个赋值，是更改环境变量，这样的好处是只针对本次运行有效，不会更改我们默认的配置。</p>
<p><strong>-ldflags参数的使用和gdb调试</strong><br>
设置编译参数-ldflags &ldquo;-w -s&rdquo;<br>
其中-w为去掉调试信息（无法使用gdb调试），-s为去掉符号表<br>
<code># go build -ldflags &quot;-w -s&quot; ./hello.go</code><br>
<code># gdb ./main</code><br>
这样就可以进行gdb调试。</p>
<p><strong>golang删除二进制文件中的源码路径信息 -trimpath</strong><br>
<code># env CGO_ENABLED=0 go build -trimpath -ldflags &quot;-s -w&quot; -o bin/frps ./cmd/frps</code></p>
<p>查询所有build用法使用这个命令<br>
<code>go help build</code></p>
<p><strong>CGO_ENABLED 环境变量</strong></p>
<p>系统默认为1，可以使用<code>go env</code>命令查看。</p>
<p>当CGO_ENABLED=1，进行编译时会将文件中引用libc的库（比如常用的net包），以动态链接的方式生成目标文件。</p>
<p>当CGO_ENABLED=0，进行编译时则会把在目标文件中未定义的符号（外部函数）一起链接到可执行文件中。</p>
<p><code># CGO_ENABLED=1 go build hello.go</code></p>

		</div>
	</div>
</div>


<div class="container">
    
    <div class="row">
        
        <div class="col-5">
            <a class="page-link link-dark text-end dh" href="/post/gpio%E7%9B%B8%E5%85%B3/">
                <h5>前一篇</h5><br>
                GPIO相关知识
            </a>            
        </div>
        
        <div class="col-2">
        </div>
        
        <div class="col-5">
            <a class="page-link link-dark text-start dh" href="/post/golang%E5%92%8Cnodejs%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE/">
                <h5>后一篇</h5><br>
                golang和nodejs环境配置
            </a>            
        </div>
        
    </div>
    
</div>

        </div><br><br>
<footer class="container">
    <h2>友情链接</h2>
    <hr>
    <nav class="nav nav-pills flex-column flex-sm-row">
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/gnzg/index.html" target="_blank">孤鸟之歌</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/mm/index.html" target="_blank">生成随机字符</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/md/index.html" target="_blank">MarkDown</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://gitee.com/" target="_blank">Gitee</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://github.com/" target="_blank">Github</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.aliyun.com/" target="_blank">阿里云</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://cloud.tencent.com/" target="_blank">腾讯云</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.oschina.net/" target="_blank">OSCHINA开源中国</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://gitee.com/wiseai/the-way-to-go_ZH_CN/blob/master/eBook/directory.md" target="_blank">the way to go</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://topgoer.com/" target="_blank">golang文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://goframe.org/display/gf" target="_blank">GoFrame</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.aliyundrive.com/" target="_blank">阿里云盘</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://cn.vuejs.org/" target="_blank">vue3文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://element-plus.gitee.io/zh-CN/" target="_blank">element-plus文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.runoob.com/vue3/vue3-tutorial.html" target="_blank">vue3菜鸟教程</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://v5.bootcss.com/" target="_blank">bootstrap v5 中文文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.bootstrap.cn/" target="_blank">bootstrap文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://caddy2.dengxiaolong.com/docs/" target="_blank">caddy中文教程</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.58pic.com/" target="_blank">千图</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://ifonts.com/" target="_blank">iFonts</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://marketing.qiniu.com/" target="_blank">七牛云</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.ixigua.com/" target="_blank">西瓜视频</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/yugang/index.html" target="_blank">鱼缸计算</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://justcc.mengkang.net/#/" target="_blank">C语言JustCC</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/pptist/" target="_blank">PPTist</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="/index.xml" target="_blank">RSS</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.kancloud.cn/idcpj/python/418553" target="_blank">参考资料</a>
        
    </nav>

    <div class="copyright text-center">
      <span class="power-by">
        Powered by <a class="links" href="https://gohugo.io" target="_blank">Hugo</a>
    </span>
    <span>|</span>
    <span>
        Theme - <a class="links" href="https://github.com/wiseai-go/blog-hugo" target="_blank">WiseAI</a>
    </span>
    <br>
    <span class="copyright-year">
        &copy;
        
        2017 -
        2023<span>
            陇ICP备15000157号
            
        </span></span>

</div>
</footer>
<script src="/js/bootstrap.bundle.min.js"></script>


</body>
</html>
